function createFloatingButton() {
    const div = document.createElement('div');
    div.className = 'floating-button';
    div.textContent = '添加答案';
  
    div.addEventListener('click', () => {
      createWindow();
    });
  
    document.body.appendChild(div);
  
    // 添加删除所有窗口的按钮
    const deleteButton = document.createElement('div');
    deleteButton.className = 'delete-all-button';
    deleteButton.textContent = '删除所有窗口';
  
    deleteButton.addEventListener('click', () => {
      deleteAllWindows();
    });
  
    document.body.appendChild(deleteButton);
  }
  
  function createWindow() {
    const windowDiv = document.createElement('div');
    windowDiv.className = 'draggable-window';
    windowDiv.textContent = prompt('请输入答案...');
  
    let isDragging = false;
    let offsetX, offsetY;
  
    windowDiv.addEventListener('mousedown', (e) => {
      isDragging = true;
      offsetX = e.clientX - windowDiv.offsetLeft;
      offsetY = e.clientY - windowDiv.offsetTop;
    });
  
    document.addEventListener('mousemove', (e) => {
      if (isDragging) {
        windowDiv.style.left = `${e.clientX - offsetX}px`;
        windowDiv.style.top = `${e.clientY - offsetY}px`;
      }
    });
  
    document.addEventListener('mouseup', () => {
      isDragging = false;
      saveWindowPosition(windowDiv);
    });
  
    windowDiv.addEventListener('input', () => {
      saveWindowPosition(windowDiv);
    });
  
    document.body.appendChild(windowDiv);
    saveWindowPosition(windowDiv);
  }
  
  function saveWindowPosition(windowDiv) {
    const windows = JSON.parse(localStorage.getItem('windows')) || [];
    const existingWindow = windows.find(w => w.id === windowDiv.id);
  
    if (!windowDiv.id) {
      windowDiv.id = `window-${Date.now()}`;
    }
  
    const windowData = {
      id: windowDiv.id,
      left: windowDiv.style.left,
      top: windowDiv.style.top,
      content: windowDiv.textContent
    };
  
    if (existingWindow) {
      existingWindow.left = windowData.left;
      existingWindow.top = windowData.top;
      existingWindow.content = windowData.content;
    } else {
      windows.push(windowData);
    }
  
    localStorage.setItem('windows', JSON.stringify(windows));
  }
  
  function restoreWindows() {
    const windows = JSON.parse(localStorage.getItem('windows')) || [];
  
    windows.forEach(windowData => {
      const windowDiv = document.createElement('div');
      windowDiv.className = 'draggable-window';
      windowDiv.id = windowData.id;
      windowDiv.style.left = windowData.left;
      windowDiv.style.top = windowData.top;
      windowDiv.textContent = windowData.content;
  
      let isDragging = false;
      let offsetX, offsetY;
  
      windowDiv.addEventListener('mousedown', (e) => {
        isDragging = true;
        offsetX = e.clientX - windowDiv.offsetLeft;
        offsetY = e.clientY - windowDiv.offsetTop;
      });
  
      document.addEventListener('mousemove', (e) => {
        if (isDragging) {
          windowDiv.style.left = `${e.clientX - offsetX}px`;
          windowDiv.style.top = `${e.clientY - offsetY}px`;
        }
      });
  
      document.addEventListener('mouseup', () => {
        isDragging = false;
        saveWindowPosition(windowDiv);
      });
  
      windowDiv.addEventListener('input', () => {
        saveWindowPosition(windowDiv);
      });
  
      document.body.appendChild(windowDiv);
    });
  }
  
  function deleteAllWindows() {
    const windows = document.querySelectorAll('.draggable-window');
    windows.forEach(windowDiv => {
      document.body.removeChild(windowDiv);
    });
  
    localStorage.removeItem('windows');
  }
  
  // 监听消息，创建窗口
  browser.runtime.onMessage.addListener((message) => {
    if (message.action === 'createWindow') {
      createWindow();
    }
  });
  
  // 初始化时创建浮动按钮并恢复窗口
  createFloatingButton();
  restoreWindows();